<?php
class Itc_Compressor {
	
	/**
	 * Files base path (public dir)
	 * @var string
	 */
	protected $basePath;
	
	/**
	 * Compresed file dir
	 * @var string
	 */
	protected $path;
	
	/**
	 * Web path
	 * @var string
	 */
	protected $webpath;
	
	/**
	 * Files
	 * @var array
	 */
	protected $files;
	
	/**
	 * File name
	 * @var String
	 */
	protected $fileName;
	
	/**
	 * Gzip level
	 * @var string
	 */
	protected $gzCompresion = 'w9';
	
	/**
	 * JsMinifile
	 * @var unknown_type
	 */
	protected $minifed = false;
	
	/**
	 * Constructor
	 * @param array $files
	 * @param Config $config
	 */
	public function __construct($config = array(), $files = array())
	{
		$this->setConfig($config);
		$this->setFiles($files);
	}
	
	/**
	 * Set config
	 * @param config $config
	 */
	public function setConfig($config)
	{
		$this->minifed = $config->minifed;
		$this->basePath = $config->basePath;
		$this->path = $this->basePath . $config->path;
		$this->webpath = $config->webpath;
	}
	
	/**
	 * Set files
	 * @param unknown_type $files
	 */
	public function setFiles($files)
	{
		$this->files = array_unique($files);
	}
	
	/**
	 * Compresed files
	 */
	public function compresed($type){
		if (empty($this->files)) return false;
		$size = 0;
		$fileName = '';
		foreach($this->files as $v){
			$fileName .= $v;
			if (!file_exists($this->basePath . $v)){
				throw new Exception('Files not found');
			}
			$size += filesize($this->basePath . $v);
		}
		
		$this->fileName = md5($fileName . $size) . '.' . $type;
		
		if(1==2 && file_exists($this->path . $this->fileName)){
			return true;
		}else{
			$this->generate($type);
		}
	}
	
	/**
	 * Gziped file
	 */
	protected function gziped($data)
	{
		$zp = gzopen($this->path . $this->fileName . '.gz', $this->gzCompresion);
		gzwrite($zp, $data);
		gzclose($zp);
	}
	
	/**
	 * Generate file
	 * @param string $method
	 */
	protected function generate($method)
	{
		$mname = 'generate' . $method;
		if (method_exists($this, $mname)){
			$data = $this->$mname();
			$this->gziped($data);
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * Generate JS file
	 */
	protected function generateJs()
	{
		$file = '';
		foreach($this->files as $v){
			$file .= file_get_contents($this->basePath . $v) . "\n";//PHP_EOL;
		}
		if ($this->minifed){
			$file = Itc_Utils_JSMin::minify($file);
		}
		file_put_contents($this->path . $this->fileName, $file);
		return $file;
	}
	
	/**
	 * Generate Css file
	 */
	protected function generateCss()
	{
		$file = '';
		foreach($this->files as $v){
			$file .= file_get_contents($this->basePath . $v) . PHP_EOL;
		}
		if ($this->minifed){
			$file = Itc_Utils_CssMin::process($file);
		}
		file_put_contents($this->path . $this->fileName, $file);
		return $file;
	}
	
	/**
	 * Get compresed file name
	 * @param bool $web
	 * @param bool $gz
	 */
	public function getFileName($web = true, $gz = true)
	{
		$filename = $gz?$this->fileName . '.gz':$this->fileName;
		return $web?$this->webpath . $filename:$filename;
	}
}